#ifndef TC_ENVELOPE_HPP
#define TC_ENVELOPE_HPP

/** \file Envelope.hpp
 * Definition of the Envelope function template.
 *
 * (C) Copyright 2003-2006 by Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: Envelope.hpp 110 2006-06-19 03:38:33Z semihc $
 *
 *
 */

#include "TA.hh"
#include "DBC.hpp"



namespace TC {

  //! Envelope function
  TC_EXPORT template <typename InputIterator,
                      typename OutputIterator,
                      class Adaptor>
  void Envelope(InputIterator fromPos,
		InputIterator fromEnd,
		OutputIterator toPosLo,
		OutputIterator toPosHi,
		const TAfp_t percent,
		const Adaptor adaptor)
  {
    TC_PRE_CONDITION(percent > 0.0);
    TC_PRE_CONDITION(percent < 1.0);
    
    // Current value
    register TAfp_t val;
    // Envelope value
    register TAfp_t eval;


    while (fromPos != fromEnd) {
      // Get current value
      val = adaptor(*fromPos++);
      // Calculate bottom (low) envelope
      eval = val * (1.0-percent);
      // Store it
      *toPosLo++ = eval;
      // Calculate top (high) envelope
      eval = val * (1.0+percent);
      // Store it
      *toPosHi++ = eval;
    }
  }



} // end namespace


#endif /* Include guard */
